package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;


import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘昱江
 * 时间 2021/5/11
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public String login(User user) {
        String password = user.getPassword();
        String md5 = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(md5);
        QueryWrapper<User> wrapper = new QueryWrapper(user);
        User userDB = userMapper.selectOne(wrapper);
        if(userDB == null || userDB.getStatus()==false ){
            return null;
        }
        String token = UUID.randomUUID().toString().replace("-", "");
        System.out.println(token);
        return token;

    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        /**1.定义分页的对象,MP提供的接口对象*/
        Page<User> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        /**2.定义条件构造器，动态sql,  StringUtils.hasLength()是spring提供的工具，用于判断字符串是否为null*/
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(flag,"username", pageResult.getQuery());
        /**3.进行分页查询,查询到的结果封装到page对象里*/
        Page<User> Page = userMapper.selectPage(page, wrapper);
        /**4.MP已经将数据封装到page对象中，获取我们需要的值给 pageResult的属性赋值即可*/
        pageResult.setRows(page.getRecords())
                .setTotal(page.getTotal());
        return pageResult;
    }
//    @Override
//    public PageResult getUserList(PageResult pageResult) {
//        long total = userMapper.selectCount(null);
//        Integer size = pageResult.getPageSize();
//        Integer start = (pageResult.getPageNum()-1)*size;
//       List<User> userList = userMapper.selectThePage(start,size);
//       pageResult.setRows(userList);
//       pageResult.setTotal(total);
//        return pageResult;
//    }
@Override
@Transactional
public void updateStatus(User user) {
    userMapper.updateById(user);

}

    @Override
    @Transactional
    public void addUser(User user) {
        String password = user.getPassword();
        String password1 = DigestUtils.md5DigestAsHex(password.getBytes())
                                        .toString().replace("-", "");
        user.setPassword(password1)
                .setStatus(true);
        /**创建时间和修改时间，公共的属性，不用一次一次添加*/
//                .setCreated(new Date())
//                .setUpdated(user.getCreated());
        userMapper.insert(user);
    }
    @Override
    public User findUserById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }
    /**
     *@Transactional是spring提供的事务管理注解
     *        规则：
     *        1.如果发生运行时异常时，则实现事务回滚
     *        2.如果发生编译时(检查异常)，默认规则事务不回滚
     *        3.属性说明：可以在注解中添加属性更改默认的设置
     *        noRollbackFor = {} 遇到某种事务不回滚
     *        rollbackFor = {} 遇到某种事务回滚
     */
    @Override
    @Transactional
    public void MedeleteById(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void updateUser(User user) {

        userMapper.updateById(user);
    }


}
